﻿<!---
 
  Copyright (c) 2010, Cristian Costantini - cristian@millemultimedia.it
  
  FileManager is licensed under the terms of the Open Source GPL 3.0 license. 
	
	http://www.gnu.org/licenses/gpl.html
  
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
--->
<cfcomponent output="false" extends="fileManager.util.Base">

	<cffunction name="init" access="public" output="false" returntype="PluginLoader">
		<cfargument name="config" type="string" required="false" default="/fileManager/config/Plugin.json" />
        <cfargument name="eventManager" type="fileManager.event.SystemEventManager" />
        <cfargument name="sessionMgr" type="fileManager.util.SessionManager" />
		
		<cfscript>
		variables.eventManager = arguments.eventManager ;
		variables.sessionMgr = arguments.sessionMgr;
		
		structAppend( variables, deserializeJSON( readFile( expandPath( arguments.config ) ) ) );
		
		</cfscript>
		<cfreturn this />
	</cffunction>

	<cffunction name="loadPlugin" access="public" returntype="void" >
		<cfset var item = 0 />
		<cfset var name = "" />
		<cfset var plugin = "" />
		<cfset var pluginConfig = "" />
		
		<cfloop index="item" from="1" to="#arrayLen( variables.plugins )#">
		
			<cfif variables.plugins[ item ].status >
			
				<cfset name = lcase( variables.plugins[ item ].name ) />
			
				<cfset pluginConfig = xmlParse( readFile( expandPath( '/fileManager/plugin/#name#/Plugin.xml' ) ) ) />
				
				<cfset plugin = createObject( 'component', 'fileManager.plugin.#name#.Plugin' ).init( pluginConfig, variables.eventManager, variables.sessionMgr ) />
		
				<cfset variables.eventManager.notifyOnPluginLoad( name=name, plugin=plugin, pluginConfig=pluginConfig ) />
		
			</cfif>
		
		</cfloop>
		
	</cffunction>
	
</cfcomponent>